/*
 * @Author: yc
 * @Date: 2020-05-26 20:52:16
 * @LastEditTime: 2020-05-26 20:53:48
 * @FilePath: \server\models\userModel.js
 */
const mongoose = require('mongoose')
const md5 = require('blueimp-md5')

const userSchema = new mongoose.Schema({
  username: {type: String, required: true}, // 用户名
  password: {type: String, required: true}, // 密码
  create_time: {type: Number, default: Date.now},
})

// 3. 定义Model(与集合对应, 可以操作集合)
const UserModel = mongoose.model('users', userSchema)

// 初始化默认超级管理员用户: admin/admin
UserModel.findOne({username: 'admin'}).then(user => {
  if(!user) {
    UserModel.create({username: 'admin', password: md5('admin')})
      .then(user => {
        console.log('初始化用户: 用户名: admin 密码为: admin')
      })
  }
})

// 4. 向外暴露Model
module.exports = UserModel